// pages/download-logs/download-logs.js
Page({
  data: {
    // 下载状态
    downloadState: 'INIT',
    downloadProgress: 0,
    statusDescription: '正在初始化下载...',
    
    // 错误信息
    errorTitle: '',
    errorMessage: '',
    
    // 下载状态映射
    stateDescriptions: {
      'INIT': '正在初始化下载...',
      'DOWNLOAD': '正在从设备下载日志文件...',
      'PREPARING': '正在准备文件...',
      'READY': '文件已准备就绪！',
      'ERROR': '下载失败'
    },
    
    // 下载间隔
    downloadInterval: null
  },

  onLoad: function (options) {
    this.startDownload();
  },

  onUnload: function () {
    this.clearDownloadInterval();
  },

  // 开始下载
  startDownload: function () {
    this.setData({
      downloadState: 'INIT',
      downloadProgress: 0,
      statusDescription: this.data.stateDescriptions.INIT,
      errorTitle: '',
      errorMessage: ''
    });

    // 模拟下载过程
    this.simulateDownload();
  },

  // 模拟下载过程
  simulateDownload: function () {
    let progress = 0;
    this.data.downloadInterval = setInterval(() => {
      progress += Math.random() * 8 + 2; // 2-10% 的随机进度
      
      if (progress >= 100) {
        progress = 100;
        this.clearDownloadInterval();
        this.downloadComplete();
      }

      let state = 'DOWNLOAD';
      if (progress > 80) {
        state = 'PREPARING';
      }

      this.setData({
        downloadProgress: Math.floor(progress),
        downloadState: state,
        statusDescription: this.data.stateDescriptions[state]
      });
    }, 300);
  },

  // 下载完成
  downloadComplete: function () {
    this.setData({
      downloadState: 'READY',
      downloadProgress: 100,
      statusDescription: this.data.stateDescriptions.READY
    });
  },

  // 清除下载间隔
  clearDownloadInterval: function () {
    if (this.data.downloadInterval) {
      clearInterval(this.data.downloadInterval);
      this.data.downloadInterval = null;
    }
  },

  // 分享文件
  shareFile: function () {
    wx.showActionSheet({
      itemList: ['发送给朋友', '分享到朋友圈', '复制链接', '保存到相册'],
      success: (res) => {
        switch (res.tapIndex) {
          case 0:
            this.shareToFriend();
            break;
          case 1:
            this.shareToMoments();
            break;
          case 2:
            this.copyLink();
            break;
          case 3:
            this.saveToAlbum();
            break;
        }
      }
    });
  },

  // 发送给朋友
  shareToFriend: function () {
    wx.showToast({
      title: '分享功能开发中',
      icon: 'none'
    });
  },

  // 分享到朋友圈
  shareToMoments: function () {
    wx.showToast({
      title: '分享功能开发中',
      icon: 'none'
    });
  },

  // 复制链接
  copyLink: function () {
    wx.setClipboardData({
      data: 'https://example.com/logs/device_logs.xed',
      success: () => {
        wx.showToast({
          title: '链接已复制',
          icon: 'success'
        });
      }
    });
  },

  // 保存到相册
  saveToAlbum: function () {
    wx.showToast({
      title: '保存功能开发中',
      icon: 'none'
    });
  },

  // 保存文件
  saveFile: function () {
    wx.showLoading({
      title: '正在保存...'
    });

    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '文件已保存',
        icon: 'success'
      });
    }, 2000);
  },

  // 重试下载
  retryDownload: function () {
    this.startDownload();
  },

  // 取消下载
  cancelDownload: function () {
    wx.showModal({
      title: '确认取消',
      content: '确定要取消下载吗？',
      success: (res) => {
        if (res.confirm) {
          this.clearDownloadInterval();
          wx.navigateBack();
        }
      }
    });
  },

  // 显示错误
  showError: function (title, message) {
    this.clearDownloadInterval();
    this.setData({
      downloadState: 'ERROR',
      errorTitle: title,
      errorMessage: message
    });
  },

  // 页面分享
  onShareAppMessage: function () {
    return {
      title: '设备日志文件',
      path: '/pages/download-logs/download-logs',
      imageUrl: '/images/share-logs.png'
    };
  }
});
